﻿using MasterMasterMode.Common.Systems;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace MasterMasterMode.Content.NPCs
{
    public class MiniShark : ModNPC
    {
        private int soundCooldown;
        private int reload_gun;
        private int rev_build;
        private const int MinSoundInterval = 500;
        private const int MaxSoundInterval = 1200;
        private const int SoundChance = 3;
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[Type] = Main.npcFrameCount[NPCID.Arapaima];
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Times.DayTime,
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Ocean,
                new FlavorTextBestiaryInfoElement("Mods.MasterMasterMode.Bestiary.MiniShark"),
            ]);
        }
        public override void SetDefaults()
        {
            NPC.lifeMax = 67;
            NPC.damage = 0;
            NPC.defense = 12;
            NPC.knockBackResist = 0.8f;
            NPC.width = 38;
            NPC.height = 28;
            NPC.aiStyle = 16;
            AIType = NPCID.Arapaima;
            AnimationType = NPCID.Arapaima;
            NPC.npcSlots = 0.8f;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.value = Item.buyPrice(0, 1, 0, 0);
            NPC.noGravity = true;
            soundCooldown = Main.rand.Next(MinSoundInterval, MaxSoundInterval + 1);
        }
        public override void ApplyDifficultyAndPlayerScaling(int numPlayers, float balance, float bossAdjustment)
        {
            NPC.lifeMax = (int)(NPC.lifeMax * 1.2f);
            NPC.damage = (int)(NPC.damage * 1.2f);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            rev_build = 0;
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 0.7f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 0.7f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 0.7f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 0.7f);
                }
                Gore.NewGore(NPC.GetSource_Death(), NPC.position, NPC.velocity, Mod.Find<ModGore>("Shell").Type, 1f);
            }
            if (NPC.life >= 0)
            {
                for (int j = 0; j < 20; j++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Smoke, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                    reload_gun = 0;
                }
            }
        }
        public override void AI()
        {
            NPC.TargetClosest();
            Player player = Main.player[NPC.target];
            HandleDolphinSounds();
            if (player.active && !player.dead)
            {
                Vector2 direction = player.Center - NPC.Center;
                float targetRotation = direction.ToRotation();
                if (direction.X < 0)
                {
                    NPC.rotation = targetRotation + (float)Math.PI;
                    NPC.spriteDirection = -1;
                }
                else
                {
                    NPC.rotation = targetRotation;
                    NPC.spriteDirection = 1;
                }
            }

            reload_gun++;
            float distance = Vector2.Distance(player.Center, NPC.Center);
            if (reload_gun > 10 && distance < 2000f && player.active && !player.dead)
            {
                ShootBullet(player);
                reload_gun = 0;
                rev_build = Math.Min(rev_build + 1, 4);
            }

            if (player.dead)
            {
                reload_gun = 0;
                rev_build = 0;
            }
        }
        private void HandleDolphinSounds()
        {
            soundCooldown--;
            if (soundCooldown <= 0)
            {
                if (Main.rand.NextBool(SoundChance))
                {
                    SoundEngine.PlaySound(SoundID.Zombie109, NPC.Center);
                }
                soundCooldown = Main.rand.Next(MinSoundInterval, MaxSoundInterval + 1);
            }
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (ModContent.GetInstance<MasterMasterModeSystem>().MasterMasterMode)
            {
                if (spawnInfo.Water)
                {
                    if (spawnInfo.Player.ZoneBeach)
                    {
                        return 0.3f;
                    }
                    return 0.1f;
                }
            }

            return 0f;
        }

        private void ShootBullet(Player player)
        {
            float offsetDistance = NPC.width / 2;
            Vector2 shootOffset;
            if (NPC.spriteDirection == 1)
            {
                shootOffset = NPC.rotation.ToRotationVector2() * offsetDistance;
            }
            else
            {
                shootOffset = (NPC.rotation - (float)Math.PI).ToRotationVector2() * offsetDistance;
            }
            Vector2 shootPosition = NPC.Center + shootOffset;
            Vector2 direction = (player.Center - shootPosition).SafeNormalize(Vector2.Zero);
            float accuracy = 0.2f / (rev_build + 1);
            direction = direction.RotatedByRandom(accuracy);
            float speed = 8f + rev_build * 0.5f;
            Vector2 shellVelocity = -direction * 3f + new Vector2(0, -2f);
            Gore.NewGore(NPC.GetSource_FromAI(), shootPosition, shellVelocity, Mod.Find<ModGore>("Shell").Type, 0.77f);
            if (Main.netMode != NetmodeID.MultiplayerClient)
            {
                int projectile = Projectile.NewProjectile(NPC.GetSource_FromAI(), shootPosition, direction * speed, ProjectileID.Bullet, NPC.damage > 0 ? NPC.damage / 2 : 5, 2f, Main.myPlayer);
                Main.projectile[projectile].friendly = false;
                Main.projectile[projectile].hostile = true;
                Main.projectile[projectile].timeLeft = 300;
                Main.projectile[projectile].rotation = direction.ToRotation();
                Main.projectile[projectile].spriteDirection = direction.X > 0 ? 1 : -1;
            }
            SoundEngine.PlaySound(SoundID.Item11, shootPosition);
        }
    }
}
